﻿/********************************************************************************/
// <copyright file="NewsRepository.cs" company="Asia E-Business Solutions">
//     Copyright © 2012. All right reserved
// </copyright>
// <history>
//     <change who="Phuoc Le" date="24/12/2012 3:02:12 PM">Created</change>
// </history>
/********************************************************************************/

using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using JLL.China.Model.DTO;

namespace JLL.China.Infrastructure.Repositories.News
{
    public class NewsRepository : SQLRepositoryBase<Model.News, int>, INewsRepository
    {
      
        public override IList<Model.News> FindAll()
        {
            var news = DataContext.NewsContents.OrderBy(x => x.datecreated).ToList();
            if (news != null)
            {
                var allNews = new List<Model.News>();
                foreach (var n in news)
                {
                    allNews.Add(NewsFactory.FromLINQObject(n));
                }
                return allNews;
            }
            else
            {
                return new List<Model.News>();
            }
        }

        public override Model.News FindBy(int key)
        {
            var news = DataContext.NewsContents.SingleOrDefault(x => key==x.NewsContent_ID);

            if (news != null)
                return NewsFactory.FromLINQObject(news);
            else
                return null;
        }
        public Model.News FindByID_Language(decimal? id,  decimal languageId)
        {
            if (id == null) return null;
            var news = DataContext.NewsContents.SingleOrDefault(x => id == x.NewsContent_ID && x.LanguageFID == languageId);

            if (news != null)
                return NewsFactory.FromLINQObject(news);
            else
                return null;
        }

        public decimal CreateNews(Model.News news)
        {
            try
            {
                return DataContext.sp_news_create(news.NewsId, news.NewCategoryFID, news.LanguageFID, news.Title,
                                              news.Summary, news.Content, news.Slug, news.Status, news.Thumbnail,
                                              news.DateCreated, news.CreatedBy, news.DateUpdated, news.UpdatedBy);
            }
            catch (Exception exception)
            {

                return 0;
            }
            
        }

        public Model.News FindById(decimal? Id)
        {
            throw new NotImplementedException();
        }

        public decimal UpdateNews(Model.News news)
        {
            try
            {
                return DataContext.sp_news_update(news.NewsId, news.NewCategoryFID, news.LanguageFID, news.Title,
                                              news.Summary, news.Content, news.Slug, news.Status, news.Thumbnail,
                                              news.DateCreated, news.CreatedBy, news.DateUpdated, news.UpdatedBy);
            }
            catch (Exception)
            {
                return 0;
            }
            
        }

        public decimal DeleteNews(decimal? newsId)
        {
            if (newsId == null) return -1;
            return DataContext.sp_news_delete(newsId);
        }

        public Binary GetThumbnail(decimal newsId)
        {
            var thumbnail = DataContext.f_GetNewsThumbnail(newsId);
            return thumbnail;
        }

        protected override void PersistDeletedItem(JLL.China.Model.News item)
        {
            var news = DataContext.NewsContents.SingleOrDefault(x => x.NewsContent_ID == item.ID);
            if (news != null)
                DataContext.NewsContents.DeleteOnSubmit(news);
        }

        protected override void PersistNewItem(Model.News item)
        {
            DataContext.NewsContents.InsertOnSubmit(NewsFactory.ToLINQObject(item));
            DataContext.SubmitChanges();
            
        }
        public int Store(Model.News n)
        {
            return 0;
          //  var id = DataContext.sp_newspost(n.ID,n.NewCategoryFID
        }

        public void ChangeStatus(string arrIds, bool status)
        {
            DataContext.sp_news_change_status(arrIds, status);
        }

        public void RemoveSelected(string arrIds)
        {
            DataContext.sp_news_remove_selected(arrIds);
        }

        protected override void PersistUpdatedItem(JLL.China.Model.News item)
        {
            var existNews = DataContext.NewsContents.SingleOrDefault(x => x.NewsContent_ID == item.ID);
            if (existNews != null)
            {
                var news = item;
                 existNews.NewCategoryFID =news.NewCategoryFID;
                 existNews.Title = news.Title;
                 existNews.NewSummary = news.Summary;
                 existNews.NewContent = news.Content;
                 existNews.Slug = news.Slug;
                 existNews.Status = news.Status;
                 existNews.Thumbnail = news.Thumbnail;
                 existNews.datecreated = news.DateCreated;
                 existNews.createdby = news.CreatedBy;
                 existNews.dateupdated = news.DateUpdated;
                 existNews.updatedby = news.UpdatedBy;
            }
            else
            {
                throw new Exception("News not found to update");
            }
        }

        public IList<Model.News> FindByNewsCategory(int ID)
        {
            var items = from m in DataContext.NewsContents
                        where m.NewCategoryFID.Equals(ID) && m.LanguageFID == 1
                        select m;
            var news = items.ToList();
            if (news != null )
            {
                var result = new List<Model.News>();
                foreach (var n in news)
                {
                    result.Add(NewsFactory.FromLINQObject(n));
                }
                return result;
            }
            else
            {
                return null;
            }
        }

        public IList<Model.News> Search(NewsSearchCriteria searchCriteria)
        {
            var items = from m in DataContext.NewsContents
                        select m;
            if (!searchCriteria.Descending)
            {
                switch (searchCriteria.OrderBy)
                {
                    case NewsSearchOrder.Title:
                        items = items.OrderBy(x => x.Title).OrderBy(x => x.Title);
                        break;
                    case NewsSearchOrder.Status:
                        items = items.OrderBy(x => x.Status);
                        break;
                    default:
                        items = items.OrderBy(x => x.datecreated);
                        break;
                }
            }
            else
            {
                switch (searchCriteria.OrderBy)
                {
                    case NewsSearchOrder.Title:
                        items = items.OrderByDescending(x => x.Title).OrderBy(x => x.Title);
                        break;
                    case NewsSearchOrder.Status:
                        items = items.OrderByDescending(x => x.Status);
                        break;
                    default:
                        items = items.OrderByDescending(x => x.datecreated);
                        break;
                }
            }

            var list = items.Skip(searchCriteria.PageIndex * searchCriteria.Count).Take(searchCriteria.Count).ToList();
            var result = new List<Model.News>();
            foreach (var n in list)
            {
                result.Add(NewsFactory.FromLINQObject(n));
            }
            return result;
        }
    }
}
